import os

HEAD = [
    "import os\n"
    "import pandas as pd\n"
    "from time import sleep\n\n"
    "from scripts_env.runner import run\n"
    "from scripts_env.case_info import case_info\n"
    "from scripts_env.case_result import CaseResult\n"
    "from scripts_env.sim_compare import sim_compare\n\n"

    "current_path = os.path.join(os.path.dirname(__file__))\n"
    "case_num = current_path.split('/')[-1]\n"
    'record_data = os.path.join(current_path,case_num+".csv")\n'
    "exp_image_path = os.path.join(current_path,'expect_image')\n"
    "run_image_path = os.path.join(current_path,'runtime_image')\n\n"
    "run(record_data, run_image_path)\n"
    "sleep(0.5)\n"
    "image_name = case_info(record_data,exp_image_path)\n"
]


def create_scripts(record_data, algo=1):
    current_path = os.path.join(os.path.dirname(record_data))
    case_num = current_path.split('/')[-1]
    exp_image_path = os.path.join(current_path, 'expect_image')
    run_image_path = os.path.join(current_path, 'runtime_image')
    record_data = os.path.join(current_path)  # , case_num+".csv")
    image_compare = "sim_compare(exp_image_path, run_image_path, record_data, image_name," + str(algo)+")"
    script = []
    script.insert(0, HEAD[0])
    script.append(image_compare)
    result = "CaseResult(record_data).get_result()"
    exit_case = "os._exit(0)"
    script.append(result)
    script.append(exit_case)
    # print(script)
    script_name = os.path.join(current_path, case_num + ".py")
    with open(script_name, 'w') as f:
        f.seek(0)
        f.write("\n".join('%s' % a for a in script))
        f.truncate()
